/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.component.jpanel;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
 * 说明：
 * 以图片做背景的面板
 */
public class ImagePanel extends JPanel {

	private static final long serialVersionUID = 1L;

	/**
	 * 背景图片
	 */
	private Image img = null;
	
	public ImagePanel() {}
	public ImagePanel(LayoutManager layout) {
		super(layout);
	}

	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (img != null) {
			int x = 0;
			int width = img.getWidth(null);
			if (width > 0) {
				while(true) {
					if (x > this.getWidth()) {
						break;
					}
					g.drawImage(img, x, 0, null);
					x += width;
				}
			}
		}
		
	}


	/**
	 * 根据传进来的图片布满，panel的背景，参数：图片
	 */
	public void setImage(Image img) {
		this.img = img;
	}
	/**
	 * 根据传进来的图片布满，panel的背景,参数：图片路径
	 */
	public void setImage(String path) {
		try {
			img = ImageIO.read(new File(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {

	}
}
